const Controller = require('egg').Controller;
const RPCClient = require('@alicloud/pop-core').RPCClient;

class VodController extends Controller {
  async getVodInfo(vodId) {
    const client = await this.vodClient();
    const res = await client.request('GetPlayInfo', {
      VideoId: vodId,
    }, {});
    return res;
  }
  async getVideo() {
    const videoId = this.ctx.params.videoId;
    const dbBack = await this.app.model.Video.findById(videoId);
    if (dbBack) {
      const videoInfo = dbBack._doc;
      const vodId = videoInfo.vodVideoId;
      const vodInfo = await this.getVodInfo(vodId);
      videoInfo.vod = vodInfo;
      this.ctx.body = videoInfo;
    } else {
      this.ctx.throw(404, '视频不存在');
    }
  }
  async vodClient() {
    const regionId = 'cn-shanghai';
    const client = new RPCClient({
      accessKeyId: 'LTAI5tKxQEKrrxJGcqMG4fUg',
      accessKeySecret: 'zu3Q1T3Br34DgtxwzbCQ1JIZGJ5Ul2',
      endpoint: 'http://vod.' + regionId + '.aliyuncs.com',
      apiVersion: '2017-03-21',
    });
    return client;
  }
  async getVod() {
    const query = this.ctx.query;
    console.log('query', query);
    this.ctx.validate({
      title: { type: 'string' },
      filename: { type: 'string' },
    }, query);
    const client = await this.vodClient();
    console.log('client', client);
    const vodBack = await client.request('CreateUploadVideo', {
      Title: query.title,
      FileName: query.filename,
    }, {});
    console.log('vodBack', vodBack);
    this.ctx.body = vodBack;
  }
}
module.exports = VodController;
